// Copyright (c) 2023, zongying_cao@163.com
// All rights reserved.
//
// This software is licensed under the MIT License.
use crate::config::BASE_API_URL;
use crate::models::{CodeSource, ImgCode, LoginInfo, Response};
use crate::result::MyResult;
use gloo_net::http::Request;

pub async fn login(login_info: LoginInfo) {
    let _resp = Request::post(&format!("{}/api/auth/login", BASE_API_URL))
        .json(&login_info)
        .unwrap()
        .send()
        .await
        .unwrap();
}
pub async fn get_code_source() -> MyResult<String> {
    Ok(
        Request::get(&format!("{}/api/auth/gen_code_source", BASE_API_URL))
            .send()
            .await?
            .json::<Response<String>>()
            .await?
            .data,
    )
}
pub async fn get_img_code(code_source: &str) -> MyResult<ImgCode> {
    Ok(
        Request::post(&format!("{}/api/auth/gen_code", BASE_API_URL))
            .json(&CodeSource {
                code: code_source.to_owned(),
            })?
            .send()
            .await?
            .json::<Response<ImgCode>>()
            .await?
            .data,
    )
}
